home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekkan Dennou Club 142
/
Gekkan Dennou Club - 2000.3 Vol. 142 (Japan).7z
/
Gekkan Dennou Club - 2000.3 Vol. 142 (Japan) (Track 1).bin
/
tools
/
pws
/
pws011.lzh
/
PersonalWS.c
< prev
next >
Wrap
C/C++ Source or Header
|
2000-02-01
|
4KB
|
188 lines
/* PersonelWS.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/xglob.h>
#include <sys/dos.h>
#define GLOBAL_DEFINE /* グローバル変数を確保する */
#include "PersonalWS.h"
#include "ServerCore.h"
#define MY_USER_STACK 48*1024
#define MY_SUPER_STACK 32*1024
/* 偶数アドレスに整合するためにわざわざ short で宣言している */
/* char とかで宣言するとスタックの底が奇数アドレスに設定されたりされなかったり */
static short user_stack[MY_USER_STACK / 2];
static short super_stack[MY_SUPER_STACK / 2];
static char *id_str = "PersonalWS"; /* 常駐識別用文字列 */
static struct _prcctrl buff;
static int v_option = 0;
/* スタックサイズとヒープサイズを指定 */
int _stacksize = 32 * 1024;
int _heapsize = 256 * 1024;
static void usage (void)
{
printf ("パーソナルウェブサーバー PersonalWS.x ver0.11\n"
" programmed by Mitsuky <FreeSoftware>\n"
"ウェブサーバーです\n"
"使用法 : PersonalWS [option] [ベースディレクトリ]\n"
"[option]\n"
" -V : 詳細表示モード\n"
" -Q文字列 : 終了文字列(無指定時は QUIT )\n"
" -R : 常駐解除\n"
"ベースディレクトリを省略するとカレントディレクトリをベースとします\n"
"BGプロセスで動作するので Human68K ver.2 以降、\n"
"かつ CONFIG.SYS で PROCESS = の設定をする必要があります\n"
);
}
/* おしゃべりモード用の printf() (要 stdarg.h) */
int verbose_printf (const char *format,...)
{
if (v_option) {
va_list ap;
va_start (ap, format);
vprintf (format, ap);
va_end (ap);
}
return (0);
}
/* 常駐部 */
static void keepent (void)
{
for (;;) {
if (buff.your_id != 0xffff) {
/* スレッド間通信があった場合 */
int th_command;
th_command = buff.command;
buff.your_id = 0xffff; /* 送り手のスレッドIDを -1 に */
if (th_command == 0xfff9) { /* 自殺コマンド? */
ServerTini ();
_dos_kill_pr ();
}
} else {
/* スレッド間通信がなかった場合 */
if (ServerMain () < 0) {
ServerTini ();
_dos_kill_pr ();
}
_dos_change_pr ();
}
}
}
int main (int argc, char *argv[])
{
int i;
int slash_flag = 0;
struct _prcptr prc;
int my_id, your_id;
int r_option = 0;
strcpy (base_dir, "./"); /* 無指定時はカレントディレクトリ */
quit_str = "QUIT";
{
char *temp;
temp = getenv ("SLASH");
if ((temp != NULL) && (*temp == '/')) {
slash_flag = 1;
}
}
for (i = 1; i < argc; i++) {
if (('-' == *argv[i]) || ((slash_flag == 0) && ('/' == *argv[i]))) {
switch (*(argv[i] + 1)) {
case 'r':
case 'R':
r_option = !0;
break;
case 'v':
case 'V':
v_option = !0;
break;
case 'q':
case 'Q':
quit_str = argv[i] + 2;
break;
default:
usage ();
return (-1);
}
} else {
strcpy (base_dir, argv[i]);
_addlastsep (base_dir);
_toslash (base_dir);
}
}
my_id = _dos_get_pr (-2, &prc);
strcpy (prc.name, id_str);
your_id = _dos_get_pr (-1, &prc);
if (your_id < 0) { /* 常駐しているか? */
if (!r_option) {
/* 常駐していない場合は常駐 */
struct _psp *my_psp;
void *mem_top, *mem_last;
if (ServerInit ())
return (-1);
printf ("常駐します\n");
buff.length = 0;
buff.command = 0xffff;
buff.your_id = -1;
_dos_open_pr (id_str, 2, (int) &user_stack[MY_USER_STACK / 2], (int) &super_stack[MY_SUPER_STACK / 2], 0, (int) &keepent, &buff, 1);
my_psp = _dos_getpdb ();
mem_top = (int *) ((int) my_psp - 0x10);
mem_last = (int *) (*(int *) ((int) mem_top + 0x08));
_dos_keeppr ((int) mem_last - (int) mem_top - (0x10 + 0xf0), 0);
} else {
printf ("まだ常駐していません\n");
return (-1);
}
} else {
if (r_option) {
#if 0
/* 既に常駐していた場合は自殺コマンドを送る */
ServerTini ();
while (_dos_send_pr (my_id, your_id, 0xfff9, NULL, 0) == -28)
_dos_change_pr ();
#endif
int temp_sock;
char temp_str[256];
temp_sock = OpenSock ("localhost", 80);
sprintf (temp_str, "GET /%s HTTP/1.0\r\n\r\n", quit_str);
SendCommand (temp_sock, temp_str);
ReceiveMessage (temp_sock);
CloseSock (temp_sock);
printf ("常駐解除しました\n");
} else {
printf ("既に常駐しています\n");
return (-1);
}
}
return (0);
}